#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
sigset_t ksigs;
void* func(void* arg)
{
    int runidx = 0;
    while(1) {
        printf("[%s]0x%x : run %6d\n", (const char*)arg, (unsigned int)pthread_self(), runidx);

        runidx ++;
        usleep(1e6);
    }

    return NULL;
}


void* func_sig(void* arg)
{
    int signo; 
    pthread_sigmask(SIG_BLOCK, &ksigs, NULL);

    while(1) {
        sigwait(&ksigs, &signo);
        if(signo == SIGUSR1) {
            printf("signal receive : %d.\n", signo);
        }
        else {
            printf("signal receive : %s.\n", "unknow");
        }
    }

    return NULL;
}


int main()
{
    printf("pid : %u.\n", getpid());

    sigemptyset(&ksigs);
    sigaddset(&ksigs, SIGUSR1);
    pthread_sigmask(SIG_BLOCK, &ksigs, NULL);

    pthread_t pth;
    pthread_create(&pth, NULL, func_sig, NULL);
    pthread_create(&pth, NULL, func, "thread1");
    pthread_create(&pth, NULL, func, "thread2");

    usleep(1e6);
    func("threadm");
    
    return 0;
}
